package com.feihong.txgw.ui.activity.user;

import android.net.http.SslError;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.feihong.txgw.R;
import com.feihong.txgw.app.App;
import com.feihong.txgw.http.HttpTools;
import com.feihong.txgw.message.LoginMessage;
import com.feihong.txgw.ui.activity.base.BaseActivity;
import com.feihong.txgw.utils.sign.SignToken;
import com.just.agentweb.AgentWeb;

import java.util.HashMap;

import butterknife.BindView;


/**
 * 通讯100-用户协议
 * Created by PVer on 2017/9/11.
 */

public class LoginRegAgrement extends BaseActivity {

    private AgentWeb mAgentWeb;
    @BindView(R.id.rl_contantframe) RelativeLayout webviewFrame;
    @BindView(R.id.txt_pageTitle) TextView txt_pageTitle;
    @BindView(R.id.iv_back) ImageButton  iv_back;

    @Override
    public void setTheme() {

    }

    @Override
    public int setLayoutID() {
        return R.layout.activity_productinfo;
    }

    @Override
    public void onDestroy() {
        try {
            webviewFrame.removeAllViews();
            if (mAgentWeb != null) {
                mAgentWeb.clearWebCache();
                mAgentWeb.destroy();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        super.onDestroy();
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        //                bundle.putString(WebViewActivity.INTENT_TITLE,getResources().getString(R.string.login_agreement_title));

        txt_pageTitle.setText("通讯100-用户协议");
    }

    //WebViewClient
    private WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //showSystemToast(url);
            return true;
            // return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {


            return true;
            // return super.shouldOverrideUrlLoading(view, request);
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            //super.onReceivedSslError(view, handler, error);
            handler.proceed();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
        }
    };

    @Override
    protected void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    //WebChromeClient
    private WebChromeClient mWebChromeClient = new WebChromeClient() {


    };

    @Override
    public void bindData() {
        //GET 参数列表
        HashMap<String, String> params = new HashMap<>();
        String getparams = new SignToken().setSigen(params);

        String url = HttpTools.LOGIN_HELLP_REG + "?" + getparams;
        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(webviewFrame, new RelativeLayout.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .setWebChromeClient(mWebChromeClient)
                .setWebViewClient(mWebViewClient)
                .setMainFrameErrorView(R.layout.agentweb_errorlayout,R.id.agentweb_re_query_data_layout)
                .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
                .createAgentWeb()
                .ready()
                .go(url);
        //水平不显示
        mAgentWeb.getWebCreator().getWebView().setHorizontalScrollBarEnabled(false);
        //垂直不显示
        mAgentWeb.getWebCreator().getWebView().setVerticalScrollBarEnabled(false);

        //屏蔽长按弹出复制按钮
        mAgentWeb.getWebCreator().getWebView().setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onUserLogin(LoginMessage loginMessage) {

    }

}
